<?php

use app\assets\ApiAsset;
use app\assets\LayerAsset;
use app\assets\MaskedInputAsset;
use app\assets\TableAsset;
use app\models\CardAttrValue;
use app\widgets\LinkPager;
use app\models\AgentBill;
use app\models\CommissionBill;
use app\models\MultiCommissionBill;
use app\widgets\ManagerTableOp;
use app\widgets\SortColumnLink;
use yii\helpers\Html;
use yii\helpers\Url;
use dosamigos\datepicker\DatePicker;
use app\models\KeyMap;
/**
 * @var $this yii\web\View
 * @var $attrList app\models\CardAttr[]
 * @var $billList app\models\CardBill[]
 * @var $pagination yii\data\Pagination
 */

ApiAsset::register($this);
LayerAsset::register($this);
MaskedInputAsset::register($this);
TableAsset::register($this);

$this->title = '批量支付代理商佣金列表';
$this->params['breadcrumbs'][] = '财务管理';
$this->params['breadcrumbs'][] = $this->title;
?>
<?php echo Html::beginForm('?', 'get', ['class'=>'form-inline']);?>
<div class="form-group" style="width: 150px;">
    <?php echo DatePicker::widget([
        'name' => 'search_start_time',
        'options' => ['placeholder' => '开始时间'],
        'attribute' => 'date',
        'template' => '{addon}{input}',
        'value' => Yii::$app->request->get('search_start_time'),
        'clientOptions' => [
            'autoclose' => true,
            'format' => 'yyyy-mm-dd',
            'todayHighlight' => true,
        ]
    ]); ?>
</div>
<div class="form-group" style="width: 150px;">
    <?php echo DatePicker::widget([
        'name' => 'search_end_time',
        'options' => ['placeholder' => '结束时间'],
        'attribute' => 'date',
        'template' => '{addon}{input}',
        'value' => Yii::$app->request->get('search_end_time'),
        'clientOptions' => [
            'autoclose' => true,
            'format' => 'yyyy-mm-dd',
            'todayHighlight' => true,
        ]
    ]); ?>
</div>
<div class="form-group">
    <label for="search_username" class="sr-only">代理商</label>
    <?php echo Html::textInput('search_username', Yii::$app->request->get('search_username'), ['id' => 'search_username', 'class' => 'form-control', 'placeholder' => '代理商', 'style' => 'width: 120px;']);?>
</div>
<div class="form-group">
    <label for="search_status" class="sr-only">支付状态</label>
    <?php echo Html::dropDownList('search_status', Yii::$app->request->get('search_status'), KeyMap::getValues('profit_status'), ['id' => 'search_status', 'class' => 'form-control', 'prompt' => '支付状态']);?>
</div>
<div class="form-group">
    <button class="btn btn-primary btn-sm">搜索</button>
    <?php if (empty($billList)) {?>
        <button type="button" class="btn btn-success btn-sm" onclick="generateBill(this, $('[name=search_start_time]').val(),$('[name=search_end_time]').val(),$('[name=search_username]').val())">生成账单</button>
    <?php }?>
    
</div>
<?php echo Html::endForm();?>
<table class="table table-striped table-bordered table-hover">
    <thead>
    <tr>
        <th class="center">
            <label class="pos-rel">
                <input type="checkbox" class="ace" />
                <span class="lbl">编号</span>
            </label>
        </th>
        <th class="center">开始日期</th>
        <th class="center">结束日期</th>
        <th class="center">代理商</th>
        <th class="center">金额</th>
        <th class="center">创建时间</th>
        <th class="center">状态</th>
        <th class="center">明细</th>
        <th class="center">支付</th>
    </tr>
    </thead>

    <tbody>
    <?php foreach ($billList as $bill) {?>
        <tr>
            <td class="center"><label class="pos-rel"><input type="checkbox" class="ace" value="<?php echo $bill->id;?>" /><span class="lbl"><?php echo $bill->id;?></span></label></td>
            <td class="center"><?php echo Yii::$app->formatter->asDate($bill->start_time);?></td>
            <td class="center"><?php echo Yii::$app->formatter->asDate($bill->end_time);?></td>
            <td class="center"><?php echo $bill->agentname;?></td>
            <td class="center"><?php echo $bill->money;?></td>
            <td class="center"><?php echo Yii::$app->formatter->asDatetime($bill->create_time);?></td>
            <td class="center"><?php echo KeyMap::getValue('profit_status',$bill->pay_status)?></td>
            <td class="center">
                <?php echo ManagerTableOp::widget(['items' => [
                    ['icon' => 'fa fa-info-circle', 'href' => Url::to(['/admin/finance/agent-profit', 'search_start_time' => Yii::$app->formatter->asDate($bill->start_time),'search_end_time' =>Yii::$app->formatter->asDate($bill->end_time) , 'search_agent' => $bill->agentname]), 'btn_class' => 'btn btn-xs btn-default', 'color'=>'green','tip' => '账单详情'],
                    // !($bill->bills->status == AgentBill::STATUS_WAIT_CONFIRM) ?: ['icon' => 'fa fa-edit', 'onclick' => 'modifyMoney(' . $bill->bills->id . ', \'' . $bill->bills->money . '\')', 'btn_class' => 'btn btn-xs btn-warning', 'tip' => '调整金额', 'color' => 'yellow'],
                    // !($bill->bills->status == AgentBill::STATUS_WAIT_CONFIRM) ?: ['icon' => 'fa fa-check-circle', 'onclick' => 'publishBill(' . $bill->bills->id . ')', 'btn_class' => 'btn btn-xs btn-warning', 'tip' => '发布账单', 'color' => 'yellow'],
                    // !($bill->bills->status == CommissionBill::STATUS_PUBLISH) ?:['icon' => 'fa fa-check-square', 'onclick' => 'set()', 'btn_class' => 'btn btn-xs btn-success', 'tip' => '账单已发布'],
                ]]);?>
            </td>
            <td class="center">
			<?php 
				echo ManagerTableOp::widget(['items' => [
                    !($bill['pay_status'] == 1) ?:['icon' => 'fa fa-check', 'onclick' => 'setPaid(' . $bill->id . ','.$bill['agent_id'] . ','.$bill['money'].','.$bill['create_time'].','.$pay_type.','.$payment_type.','.$bill->start_time.','.$bill->end_time.')', 'btn_class' => 'btn btn-xs btn-warning', 'tip' => '提交付款'],
                    !($bill['pay_status'] == 2) ?:['icon' => 'fa fa-check', 'onclick' => 'set()', 'btn_class' => 'btn btn-xs btn-success', 'tip' => '已完成'],
                    // ['icon' => 'fa fa-trash', 'onclick' => 'deletelog('.$bill->id.')', 'btn_class' => 'btn btn-xs btn-danger','tip' => '删除', 'color'=>'red']
                ]]);
			?>

            </td>
        </tr>
    <?php }?>
    </tbody>
</table>
<?php echo LinkPager::widget(['pagination' => $pagination]);?>
<!-- <script type="text/javascript">
    function deletelog(id) {
        if (!confirm('确定要删除吗？')) {
            return false;
        }
        $.getJSON('<?php echo Url::to(['/admin/finance/delete-order']);?>', {'id':id}, function(json) {
            if (callback(json)) {
                layer.msg('删除成功', function () {window.location.reload();});
            }
        });
    }
</script> -->
<script type="text/javascript">
    function setPaid(id,agentid,money,gtime,pay_type,payment_type,start_time,end_time) {
        layer.confirm('确定要付款吗?', {icon: 3, title:'提示'}, function(index){
           $.getJSON('<?php echo Url::to(['/admin/finance/set-payment-paid']);?>', {id:id,agentid:agentid,money:money,gtime:gtime,pay_type:pay_type,payment_type:payment_type,start_time:start_time,end_time:end_time}, function (json) {
                if (callback(json)) {
                    layer.msg('提交付款申请成功，请耐心等待到账！', function () {window.location.reload();});
                }
            });
            layer.close(index);
        }); 
    }
</script>
<script>
    function generateBill(btn, start,end,name) {
        if (start == '') {
            layer.msg('请选择开始日期。', function () {});
            return;
        }
        if(end == ''){
            layer.msg('请选择结束日期',function (){});
            return;
        }
        if(name == ''){
            layer.msg('请填写代理商名称',function (){});
            return;
        }
        $(btn).prop('disabled', true);
        $.getJSON('<?php echo Url::to(['/admin/finance/generate-multi-agent-bill']);?>', {start: start,end:end,name:name}, function (json) {
            if (callback(json)) {
                layer.msg('账单生成成功！', function () {window.location.reload();});
            }
        });
    }

</script>
<script>
    /**
     * 调整账单金额
     * @param id
     * @param money
     */
    function modifyMoney(id, money) {
        var newMoney = window.prompt('请输入新的账单金额：', money);
        $.getJSON('<?php echo Url::to(['/admin/user/modify-commission-money']);?>', {id: id, money: newMoney}, function (json) {
            if (callback(json)) {
                layer.msg('金额已更新。', function () {window.location.reload();});
            }
        });
    }

    /**
     * 发布账单
     * @param id
     */
    function publishBill(id) {
        layer.confirm('确定要发布账单吗？', {icon: 3, title:'提示'}, function(index){
           $.getJSON('<?php echo Url::to(['/admin/user/publish-commission-bill']);?>', {id: id}, function (json) {
                if (callback(json)) {
                    layer.msg('账单已发布。', function () {window.location.reload();});
                }
            });
            layer.close(index);
        }); 
    }
</script>